<template>
  <a-drawer width="35%" :label-col="4" :wrapper-col="14" :visible="open" @close="onClose">
    <a-divider orientation="left">
      <b>{{ formTitle }}</b>
    </a-divider>
    <a-form-model ref="form" :model="form" :rules="rules">
      <a-form-model-item label="表名" prop="tableName">
        <a-input v-model="form.tableName" placeholder="请输入表名，请使用下划线方式命名" />
      </a-form-model-item>
      <a-form-model-item label="描述" prop="tableComment">
        <a-input v-model="form.tableComment" placeholder="请输入表描述" />
      </a-form-model-item>
      <div class="bottom-control">
        <a-space>
          <a-button type="primary" @click="submitForm">
            保存
          </a-button>
          <a-button type="dashed" @click="cancel">
            取消
          </a-button>
        </a-space>
      </div>
    </a-form-model>
  </a-drawer>
</template>

<script>

import { addGenTable } from '@/api/tool/gen'

export default {
  name: 'CreateForm',
  components: {
  },
  data () {
    return {
      loading: false,
      menuExpandedKeys: [],
      autoExpandParent: false,
      menuCheckedKeys: [],
      halfCheckedKeys: [],
      menuOptions: [],
      formTitle: '',
      // 表单参数
      form: {
        tableName: undefined,
        tableComment: undefined
      },
      open: false,
      menuExpand: false,
      menuNodeAll: false,
      rules: {
        tableName: [{ required: true, message: '表名称不能为空', trigger: 'blur' }],
        tableComment: [{ required: true, message: '表描述不能为空', trigger: 'blur' }]
      },
      defaultProps: {
        children: 'children',
        title: 'label',
        key: 'id'
      }
    }
  },
  filters: {
  },
  created () {
  },
  computed: {
  },
  watch: {
  },
  methods: {
    onClose () {
      this.open = false
    },
    // 取消按钮
    cancel () {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset () {
      this.form = {
        tableName: undefined,
        tableComment: undefined
      }
    },
     /** 新增按钮操作 */
    handleAdd () {
      this.reset()
      this.open = true
      this.formTitle = '增加数据库表'
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs.form.validate(valid => {
        if (valid) {
          addGenTable(this.form).then(res => {
            this.$message.success(
              '新增成功',
              3
            )
            this.open = false
            this.$emit('ok')
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>
